package org.gof.application.Reactor
{
	import com.iMANIT.utils.BindedFunction;
	import com.iMANIT.DS.DubLinkedList.DubLinkedList;
	
	public class Reactor
	{
		protected var reactions:Vector.<DubLinkedList>;
		public var actions:Vector.<BaseGameAction>;
		
		public function Reactor():void
		{
			
		}
		
		public function addReaction(ga:BaseGameAction, func:Function, ...params):void
		{
			
			
			var gaReactions:DubLinkedList = reactions[ga];
			
			if (gaReactions)
			{
				gaReactions.insertEnding(func);
			}
			else
			{
				gaReactions = new DubLinkedList();
				gaReactions.insertEnding(new BindedFunction(func,null, params));
			}
		}
		
		public function react():void
		{
			for (var cntx:int = 0; cntx < actions.length; cntx++)
			{
				reactToAction(actions[cntx]);
			}
		}
		
		protected function reactToAction(ga:BaseGameAction):void
		{
			var gaReactions:DubLinkedList = reactions[ga];
				
				if ((gaReactions) && (gaReactions.length >0))
				{
					var bf:BindedFunction = gaReactions.removeBegining() as BindedFunction;
					bf.params.push(ga);
					(bf.func).apply(null, bf.params);
				}
		}

	}
}